package com.example;

import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.lang.Thread;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;

import DataCrawler.CidCrawler;
import DataCrawler.CommentCrawler;
import DataCrawler.DataProcessor;
import DataProcessing.Visualizer;

/**
 * Hello world!
 *
 */
public class App {
    public static String resDir = "src\\main\\resources\\";
    public static String apiUrl = "https://comment.bilibili.com/%s.xml";

    public static void main(String[] args) {
        boolean recrawl = false;
        var commentList = new ArrayList<String>();

        if (recrawl) {
            CidCrawler cidc = new CidCrawler();
            cidc.setCookie(
                    "buvid3=E37F2A73-7A88-07C5-B7DD-7CCE71B1759F53472infoc; i-wanna-go-back=-1; _uuid=AE8B103DA-10B7B-89CD-FD10F-244567937D7E57589infoc; buvid4=221FD661-E3D9-2C03-18F1-32E950E8F25158734-022040109-NSMokBUPpjNXtme2Loy40g==; buvid_fp_plain=undefined; CURRENT_BLACKGAP=0; LIVE_BUVID=AUTO5916489524695224; blackside_state=0; rpdid=|(u))RJmYkml0J'uYR)mlkYl); nostalgia_conf=-1; hit-dyn-v2=1; DedeUserID=25301637; DedeUserID__ckMd5=5006c283b8d931f2; b_ut=5; CURRENT_QUALITY=64; PVID=1; fingerprint=a9b75dcddbeb6531c36f67ae0456f411; SESSDATA=1112f040,1668256819,e6456*51; bili_jct=ac8bd39b877a623cfd41b62a7d8efec8; sid=815mt0po; buvid_fp=a9b75dcddbeb6531c36f67ae0456f411; bp_video_offset_25301637=661024367726035000; CURRENT_FNVAL=4048; innersign=1; b_lsid=33B3822B_180D0DC9D41")
                    .setUrl("https://www.bilibili.com/video/BV1H44y1T74v?spm_id_from=333.1007.top_right_bar_window_default_collection.content.click")
                    .setResources(resDir + "Cid\\")
                    .setUserAgent(
                            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47")
                    .setPageOutputFile("Page").run();
            System.out.println("[Info] Cid爬取完成。");
            DataProcessor dp = new DataProcessor();
            dp.resDir = resDir + "Cid\\";
            dp.parseStringFromFile("Page.json");

            var cidList = dp.getCidList();

            System.out.println("[Info] 解析完成");
            int after = 1;
            for (String cid : cidList) {
                CommentCrawler comc = new CommentCrawler();
                comc
                        .setResources(resDir + "Comments\\")
                        .setPageOutputFile("Comments_" + String.valueOf(after++))
                        .setUrl(String.format(apiUrl, cid))
                        .run();
                commentList.addAll(comc.getComments());
                System.out.println(
                        "[Info] 完成了对视频" + cid + "的弹幕抓取，抓取到" + String.valueOf(comc.getComments().size()) + "条弹幕。");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.err.println("[Info] 线程被打断。");
                }
            }
        }
        else {
            String fullname = resDir + "Comments\\Comments_%d.xml";
            int after=1;
            while (true) {
                try {
                    String s = FileUtils.readFileToString(new File(String.format(fullname, after)),
                            Charset.forName("utf8"));
                    commentList.addAll(CommentCrawler.getComments(s));
                    System.out.println("[Info] 读取了" + after++ + "个弹幕文件。");
                }
                catch (IOException e) {
                    break;
                }
            }
        }
        
        System.out.println("[Info] 开始可视化");
        var v = new Visualizer(commentList);
        v.getWordCloud(resDir + "WordCloud1.png", "src\\main\\resources\\bg2.png");
        System.out.println("[Info] Done");

    }
}
